<?php
namespace Admin\Controller;
class CourseController extends AdminController
{
	/**
	 * 发布的课程列表
	 * @param  int $passed [1通过，2未通过]
	 */
	public function index($passed = ''){

		//设定默认值
		$passed = empty($passed) ? 0 : $passed;
		$_GET['passed'] = $passed;
		$lists = D('Course') -> courselist($passed);
		
		

		/*今日操作的课程数 start*/
		$today = strtotime(date("Y-m-d"));
		$where = array();
		$where['update_time'] = array('gt',$today);
		$today_op_course = D('Course')->where($where)->count();
		$this->assign('today_op_course',$today_op_course);
		/*今日操作的课程数 end*/

		/*今天通过的用户数 start*/

		$where['passed'] = 1;

		$today_pass_user = D('Course') -> where($where) ->group('uid')-> select();
		
		$today_pass_user = count($today_pass_user);
		
		
		/* $today_pass_user = M('auth')->where($where)->count(); */

		$this->assign('today_pass_user',$today_pass_user);
		
		
		
		
		/*今天通过的用户数 end*/

		/*总上架课程数 start*/
		$condi['passed'] = 1;
		$condi['status'] = 1;
		$condi['end_top'] = 0;
		$all_course = M('Course')->where($condi)->count();
		$this->assign('all_course',$all_course);
		/*总上架课程数 end*/
		
		/*有课程的用户 start 上架有课程的用户数*/
		/* $distinct_course = M('Course')->distinct(true)->field('uid')->select(); */
		
		$where1['status'] = 1;
		
		$where1['passed'] = 1;
		
		$where1['end_top'] = 0;
		
		$distinct_course = D('Course') -> where($where1) ->group('uid')-> select();

		$user_count = count($distinct_course);
		
		$this->assign('user_count',$user_count);
		
		/*有课程的用户 end*/
		
		/* yxf 20171107 时薪有几位小数就显示几位 */
		
		foreach ($lists[0] as &$val) {
			
			$val['salary'] = floatval($val['salary']);

		}
		
		unset($val);
		
		$this -> assign('lists', $lists);
		$this -> display();
	}

	/**
	 * 修改课程条目状态：通过，不通过，删除
	 * @return [type] [description]
	 */
	public function edit(){
		if (IS_AJAX) {
			$id = I('post.id');
			$val = I('post.val');
			$flag = I('post.flag');

			if (D('Course') -> edit($id, $val, $flag)) {

				/*同步修改时间start  yjmp*/
				D('Course') -> edit($id, time(), 'update_time');
				/*同步修改时间end*/

				//如果是审核课程
				if ($flag == 'passed') {
					$res = D('Course') -> find($id);
					$msg['sid']	    = 0;
					$msg['rid'] 	= $res['uid'];
					$msg['type']    = 4;

					if ($val == 1) {
						$msg['content'] = '您发布的课程已认证通过。';

						//发送信息
						if (D('Message') -> addMessage($msg)) {
							//如果课程审核通过，查找是否已有教师评价
							$result = D('Course') -> courseUid($id);

							//如果没有，则创建
							if (empty($result)) {
								$uid = D('Course') -> field('uid') -> find($id);
								if (D('Home/TeacherEva') -> add($uid)) {
									$this -> ajaxReturn('操作成功');
								} else {
									$this -> ajaxReturn('操作失败');
								}
								return;
							}
							$this -> ajaxReturn('操作成功');
						}
					} elseif ($val == 2) {
						$msg['content'] = '您发布的课程未通过。';
						//发送信息
						if (D('Message') -> addMessage($msg)) {
							$this -> ajaxReturn('操作成功');
						}
					}
				}						
			} else {
				$this -> ajaxReturn('操作失败');
			}
		}
	}


	/*添加批量审核*/
	public function ajaxedit(){
		if (IS_AJAX) {
			$ids = I('post.ids');
			$val = I('post.val');
			$flag = I('post.flag');

			if(empty($ids)){
				$this -> ajaxReturn('操作失败');
				return;
			}

			foreach($ids as $value){
				$id = 0;
				$id = $value;

				if (D('Course') -> edit($id, $val, $flag)) {
						/*同步操作时间 start  yjmp*/
						D('Course') -> edit($id, time(), 'update_time');
						/*同步操作时间  end*/
						//如果是审核课程
						if ($flag == 'passed') {
							$res = D('Course') -> find($id);
							$msg['sid']	    = 0;
							$msg['rid'] 	= $res['uid'];
							$msg['type']    = 4;

							if ($val == 1) {
								$msg['content'] = '您发布的课程已认证通过。';
								//发送信息
								if (D('Message') -> addMessage($msg)) {
									//如果课程审核通过，查找是否已有教师评价
									$result = D('Course') -> courseUid($id);
									//如果没有，则创建
									if (empty($result)) {
										$uid = D('Course') -> field('uid') -> find($id);

										if (D('Home/TeacherEva') -> add($uid)) {
											//$this -> ajaxReturn('操作成功');
										} else {
											//$this -> ajaxReturn('操作失败');
										}
										//return;
									}
									//$this -> ajaxReturn('操作成功');
								}
							} 
						}
					}
			}
			$this -> ajaxReturn('操作成功');
		}
	}
}

